package com.cg.springSecurity.security;

import com.google.gson.Gson;
import com.cg.springSecurity.base.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Component
public class CustomizeLogoutSuccessHandler implements LogoutSuccessHandler {
    @Autowired
    private Gson gson;
    @Override
    public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        String header = httpServletRequest.getHeader("Authorization");
        httpServletResponse.setContentType("text/json;charset=utf-8");
        if (header == null) {
            httpServletResponse.getWriter().write(gson.toJson(new ResultInfo<>("error！","注销失败，不合法的token")));
        } else httpServletResponse.getWriter().write(gson.toJson(new ResultInfo<>("注销成功！")));
    }
}
